using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Q.Framework.Extension;

namespace Q.Framework.WebControls
{
    public class AutoPageableGridView : GridView, IBindableControl
    {
        public object AutoBindDataSource
        {
            get { return DataSource; }
            set { DataSource = value; DataBind(); }
        }

        public string DataPropertyName { get; set; }

        private String controlBindableProperty = "AutoBindDataSource";
        public string ControlBindableProperty
        {
            get { return controlBindableProperty; }
            set { controlBindableProperty = value; }
        }

        private const string _virtualItemCount = "virtualItemCount";
        private const string _currentPageIndex = "currentPageIndex";

        [Category("Control")]
        public event BindHandle Bind;

        [Browsable(true), Category("Custom")]
        [Description("Set the virtual item count for this grid")]
        public int VirtualItemCount
        {
            get
            {
                if (ViewState[_virtualItemCount] == null)
                    ViewState[_virtualItemCount] = -1;
                return Convert.ToInt32(ViewState[_virtualItemCount]);
            }
            set
            {
                ViewState[_virtualItemCount] = value;
            }
        }

        private int CurrentPageIndex
        {
            get
            {
                if (ViewState[_currentPageIndex] == null)
                    ViewState[_currentPageIndex] = 0;
                return Convert.ToInt32(ViewState[_currentPageIndex]);
            }
            set
            {
                ViewState[_currentPageIndex] = value;
            }
        }

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;
                this.CurrentPageIndex = this.PageIndex;
            }
        }

        public void BindGrid()
        {
            Bind(0, PageSize);
        }

        protected override void OnLoad(EventArgs e)
        {
            this.PageIndexChanging += new GridViewPageEventHandler(AutoPageableGridView_PageIndexChanging);
            base.OnLoad(e);
        }

        void AutoPageableGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            PageIndex = e.NewPageIndex;
            Bind(e.NewPageIndex, PageSize);
        }
            
        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            if (CustomPaging)
            {
                pagedDataSource.AllowCustomPaging = true;
                pagedDataSource.VirtualCount = this.VirtualItemCount;
                pagedDataSource.CurrentPageIndex = this.CurrentPageIndex;
            }
            base.InitializePager(row, columnSpan, pagedDataSource);
        }

        public bool CustomPaging
        {
            get { return (this.VirtualItemCount != -1); }
        }
    }

    public delegate void BindHandle(Int32 pageIndex, Int32 PageSize);
}